/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canvas;

import java.io.IOException;
import javax.microedition.lcdui.Canvas;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import midlet.MainMidlet;
import util.SpriteClass;

/**
 *
 * @author aisyah
 */
public class MenuCanvas extends Canvas implements CommandListener {

    private MainMidlet midlet;
    private Displayable returnScreen;
    int index = 0;
    private String language;

    public MenuCanvas(MainMidlet midlet, Displayable returnScreen, String language) {
        this.midlet = midlet;
        this.returnScreen = returnScreen;
        this.language = language;

    }

    public MenuCanvas(MainMidlet midlet) {
        this.midlet = midlet;
    }

    protected void paint(Graphics g) {
        try {
            setFullScreenMode(true);
            // set background image
            SpriteClass bg = new SpriteClass(Image.createImage("/res/bg_all.png"));
            bg.setPosition(0, 0);

            SpriteClass title = new SpriteClass(Image.createImage("/res/title/title_sl.png"));
            title.setPosition(80, 50);

            SpriteClass button[] = new SpriteClass[3];
            for (int i = 0; i < 3; i++) {
                if (index % 3 == i) {
                    button[i] = new SpriteClass(Image.createImage("/res/btn/hvr/h_btn-menu-" + i + ".png"));
                } else {
                    button[i] = new SpriteClass(Image.createImage("/res/btn/btn-menu-" + i + ".png"));
                }
                button[i].setPosition(110, 100 + (40 * i));
            }

            LayerManager lmng = new LayerManager();

            for (int i = 0; i < 3; i++) {
                lmng.append(button[i]);
            }
            lmng.append(title);
            lmng.append(bg);
            lmng.paint(g, 0, 0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Called when a key is pressed.
     */
    protected void keyPressed(int keyCode) {

        if (keyCode == -1) {
            // atas
            if (index != 0) {
                index -= 1;
            }
        } else if (keyCode == -2) {
            // bawah
            index += 1;
        } else if (keyCode == -5) {
            // fire button
            actionButton();
        } else if (keyCode == -6) {
            Display.getDisplay(midlet).setCurrent(returnScreen);
        } else if (keyCode == -7) {
            this.midlet.destroyApp(true);
        }
        repaint();
    }

    private void actionButton() {
        int posisi = index % 3;
        System.out.println("posisi==========>" + posisi);
        switch (posisi) {
            case 0:
                ListSuratCanvas srtCvs = new ListSuratCanvas(midlet, Display.getDisplay(midlet).getCurrent(), language);
                Display.getDisplay(midlet).setCurrent(srtCvs);
                break;
            case 1:
                ListJuzCanvas juzCvs = new ListJuzCanvas(midlet, Display.getDisplay(midlet).getCurrent(), language);
                Display.getDisplay(midlet).setCurrent(juzCvs);
                break;
            case 2:
                System.out.println("about us");
                break;
        }

    }

    /**
     * Called when action should be handled
     */
    public void commandAction(Command c, Displayable d) {
    }
}
